<?php

namespace App\Http\Controllers\Api;

use App\Consts\TimeConst;
use App\Exceptions\ExceptionHelper;
use App\Exceptions\ExceptionSms;
use App\FFMpegUtil;
use App\Format\BaseFormat;
use App\Http\Controllers\Controller;
use App\Services\Common\FormatService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

/**
 * 上传
 * Class UploadController
 * @package App\Http\Controllers\Api
 */
class UploadController extends Controller
{
    // 单图
    public function uploadImage(Request $request)
    {
        $filename = 'file';

        if (!$request->hasFile($filename)) {
            throw ExceptionHelper::getInstance(ExceptionHelper::PLACE_UPLOAD_IMAGE);
        }
        $file = $request->file($filename);

        if (!$file->isValid()) {
            throw ExceptionHelper::getInstance(ExceptionHelper::PLACE_UPLOAD_IMAGE);
        }
        if (!in_array(strtolower($file->extension()), ['jpeg', 'jpg', 'gif', 'png'])) {
            throw ExceptionHelper::getInstance(ExceptionHelper::PLACE_UPLOAD_IMAGE,
                '图片格式需要为jpg,png,gif格式');
        }
        $datePath = Carbon::now()->format(TimeConst::DATE_FORMAT);
        $fileName = Str::random(40) . '.' . $file->getClientOriginalExtension();

        if (is_qiniu()) {
            $disk = Storage::disk('qiniu');
            $path = $disk->put('storage/image/' . $datePath, $file);
            $url  = $disk->url($path);
        } else {
            $path = $file->storeAs('image/' . $datePath, $fileName);
            $url  = BaseFormat::baseUrl('/storage/image/' . $datePath . '/' . $fileName);
        }
        return ExceptionSms::returnSuccess([
            'url' => $url
        ]);
    }

    // 多图
    public function uploadImages()
    {

    }

    // 文件
    public function uploadFile(Request $request)
    {
        $filename = 'file';
        if (!$request->hasFile($filename)) {
            throw ExceptionHelper::getInstance(ExceptionHelper::PLEASE_UPLOAD_FILE);
        }
        $file = $request->file($filename);
        if (!$file->isValid()) {
            throw ExceptionHelper::getInstance(ExceptionHelper::PLEASE_UPLOAD_FILE);
        }
        $datePath = Carbon::now()->format(TimeConst::DATE_FORMAT);
        if (is_qiniu()) {
            $disk = Storage::disk('qiniu');
            $path = $disk->put('storage/files/' . $datePath, $file);
            $url  = $disk->url($path);
        } else {
            $path = $file->store('files/' . $datePath);
            $url  = BaseFormat::baseUrl('storage/' . $path);
        }

        return ExceptionSms::returnSuccess(['url' => $url]);
    }

    // 视频
    public function uploadVideo(Request $request)
    {
        $fileName = 'file';

        if (!$request->hasFile($fileName)) {
            throw ExceptionHelper::getInstance(ExceptionHelper::PLACE_UPLOAD_VIDEO);
        }
        $file = $request->file($fileName);
        if (!$file->isValid()) {
            throw ExceptionHelper::getInstance(ExceptionHelper::PLACE_UPLOAD_VIDEO);
        }
        if (!in_array(strtolower($file->getClientOriginalExtension()), ['mp4'])) {
            throw ExceptionHelper::getInstance(ExceptionHelper::PLACE_UPLOAD_VIDEO,
                '视频仅支持mp4格式');
        }
        $datePath = Carbon::now()->format(TimeConst::DATE_FORMAT);
        $fileName = Str::random(40) . '.' . $file->getClientOriginalExtension();
        if (is_qiniu()) {
            $disk  = Storage::disk('qiniu');
            $path  = $disk->put('storage/video/' . $datePath, $file);
            $url   = $disk->url($path);
            $cover = $url . '?vframe/jpg/offset/0';
        } else {
            $path      = $file->storeAs('video/' . $datePath, $fileName);
            $steamPath = storage_path('app/' . $path);

            // 截图封面
            $cover = FFMpegUtil::getCover($steamPath, 1);
            $url   = BaseFormat::baseUrl('/storage/video/' . $datePath . '/' . $fileName);
            $cover = BaseFormat::baseUrl('/storage/video/' . $datePath . '/' . basename($cover));
        }

        return ExceptionSms::returnSuccess([
            'url'   => $url,
            'cover' => $cover,
        ]);
    }

    public function uploadVod(Request $request)
    {
        ini_set('memory_limit', -1);
        $file     = $request->file('file');
        $filename = $request->input('fileName');
        $fileext  = $request->input('fileExt');
        $chunk    = $request->input('chunk');
        $chunks   = $request->input('chunks');
        if (!in_array($fileext, ['mp4'])) {
            return ExceptionSms::returnSuccess(['status' => -1, 'msg' => '请上传mp4格式的视频']);
        }
        $path = 'temp/' . $filename . '.' . $chunk;

        $disk = Storage::disk('local');

        $disk->put($path, $file->get());

        if ($chunk >= $chunks) {
            $new_name = Str::random(40) . '.' . $fileext;
            $content  = '';
            for ($i = 0; $i < $chunks; $i++) {
                $temp    = '/temp/' . $filename . '.' . ($i + 1);
                $content .= $disk->get($temp);
                $disk->delete($temp);
            }
            $video_path = 'vod/' . Carbon::now()->format('ymd') . '/' . $new_name;
            $disk->put($video_path, $content);
            $status    = 2;
            $video_url = $disk->url($video_path);
            $steamPath = storage_path('app/' . $video_path);
            // 截图封面
            $cover = FFMpegUtil::getCover($steamPath, 1);
            $cover = BaseFormat::baseUrl('/storage/vod/' . Carbon::now()->format('ymd') . '/' . basename($cover));
        } else {
            $status = 1;
        }
        return ExceptionSms::returnSuccess([
            'status'    => $status,
            'video_url' => $video_url ?? "",
            'cover_url' => $cover ?? "",
            'chunk'     => $chunk,
            'chunks'    => $chunks,
        ]);
    }
}
